//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ImpliedVolTermStructure.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVarianceTermStructure.h>
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ origTS, DateTime referenceDate) : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
    CBlackVolTermStructure^ _CorigTS;
    try
    {
#ifdef HANDLE
        _phImpliedVolTermStructure = NULL;
#endif
        _CorigTS = safe_cast<CBlackVolTermStructure^> (origTS);
        _CorigTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _origTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CorigTS->GetHandle ()); 
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        _ppImpliedVolTermStructure = new boost::shared_ptr<QuantLib::ImpliedVolTermStructure> (new QuantLib::ImpliedVolTermStructure ( _origTS,  _referenceDate ));
        SetBlackVarianceTermStructure (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppImpliedVolTermStructure));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CorigTS != nullptr) _CorigTS->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (boost::shared_ptr<QuantLib::ImpliedVolTermStructure>& childNative, Object^ owner) : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedVolTermStructure = NULL;
#endif
	_ppImpliedVolTermStructure = &childNative;
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppImpliedVolTermStructure));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (QuantLib::ImpliedVolTermStructure& childNative, Object^ owner) : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedVolTermStructure = NULL;
#endif
	_ppImpliedVolTermStructure = new boost::shared_ptr<QuantLib::ImpliedVolTermStructure> (&childNative);
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppImpliedVolTermStructure));
    _ImpliedVolTermStructureOwner = owner;
    _BlackVarianceTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (CImpliedVolTermStructure^ copy) : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedVolTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppImpliedVolTermStructure = new boost::shared_ptr<QuantLib::ImpliedVolTermStructure> (copy->GetShared());
        _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppImpliedVolTermStructure));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (System::Type^ t) : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedVolTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CImpliedVolTermStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (QuantLib::Handle<QuantLib::ImpliedVolTermStructure>& childNative, Object^ owner)  : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
	_phImpliedVolTermStructure = &childNative;
	_ppImpliedVolTermStructure = &static_cast<boost::shared_ptr<QuantLib::ImpliedVolTermStructure>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppImpliedVolTermStructure));
    _ImpliedVolTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (QuantLib::Handle<QuantLib::ImpliedVolTermStructure> childNative)  : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
	_phImpliedVolTermStructure = &childNative;
	_ppImpliedVolTermStructure = &static_cast<boost::shared_ptr<QuantLib::ImpliedVolTermStructure>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppImpliedVolTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::CImpliedVolTermStructure (QuantLib::ImpliedVolTermStructure childNative)  : CBlackVarianceTermStructure(CImpliedVolTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedVolTermStructure = NULL;
#endif
	_ppImpliedVolTermStructure = new boost::shared_ptr<QuantLib::ImpliedVolTermStructure> (new QuantLib::ImpliedVolTermStructure (childNative));
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppImpliedVolTermStructure));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::~CImpliedVolTermStructure ()
{
    if (_ppImpliedVolTermStructure != NULL)
    {
	    delete _ppImpliedVolTermStructure;
        _ppImpliedVolTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::!CImpliedVolTermStructure ()
{
    if (_ppImpliedVolTermStructure != NULL)
    {
	    delete _ppImpliedVolTermStructure;
    }
}
QuantLib::ImpliedVolTermStructure& Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::GetReference ()
{
    if (_ppImpliedVolTermStructure == NULL) throw gcnew NativeNullException ();
	return **_ppImpliedVolTermStructure;
}
boost::shared_ptr<QuantLib::ImpliedVolTermStructure>& Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::GetShared ()
{
    if (_ppImpliedVolTermStructure == NULL) throw gcnew NativeNullException ();
	return *_ppImpliedVolTermStructure;
}
QuantLib::ImpliedVolTermStructure* Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::GetPointer ()
{
    if (_ppImpliedVolTermStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppImpliedVolTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ImpliedVolTermStructure>& Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::GetHandle ()
{
	if (_phImpliedVolTermStructure == NULL)
	{
		_phImpliedVolTermStructure = new Handle<QuantLib::ImpliedVolTermStructure> (*_ppImpliedVolTermStructure);
	}
	return *_phImpliedVolTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::HasNative () 
{
	return (_ppImpliedVolTermStructure != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppImpliedVolTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppImpliedVolTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppImpliedVolTermStructure)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppImpliedVolTermStructure)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Equityfx::IImpliedVolTermStructure^ Cephei::QL::Termstructures::Volatility::Equityfx::CImpliedVolTermStructure_Factory::Create (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ origTS, DateTime referenceDate)
{
    return gcnew CImpliedVolTermStructure ( origTS,  referenceDate);
}
